/**
 * @copyright 2015 Indie Semiconductor.
 *
 * This file is proprietary to Indie Semiconductor.
 * All rights reserved. Reproduction or distribution, in whole
 * or in part, is forbidden except by express written permission
 * of Indie Semiconductor.
 *
 * @file lin_device.h
 */

#ifndef __LIN_DEVICE_H__
#define __LIN_DEVICE_H__

#include <stdint.h>
#include <stdbool.h>
#include "rugby.h"
#include "lins_sfr.h"
#include "linm_sfr.h"

#define LIN_BUFF_SIZE                   8
#define LIN_SCH_TIME_BASE               50

/* Defines for "LIN_bus_status" states.  */ 
#define LIN_BUS_QUIET_STATE             0 
#define LIN_BUS_SYNC_RECEIVE_STATE      1 
#define LIN_BUS_IDENTIFY_RECEIVE_STATE  2 
#define LIN_BUS_DATA_RECEIVE_STATE      3 
#define LIN_BUS_DATA_SEND_STATE         4 
#define LIN_BUS_WAKEUP_SEND_STATE       5 

/* Defines for "LIN-node_status" states.  */ 
#define LIN_DISCONNECT                  0 
#define LIN_CONNECT                     1 
#define LIN_SLEEP_STATE                 2 
#define LIN_WAKE_UP_STATE               3 

/* Defines for LIN flag operations from within the API.  */
#define LIN_FLG_TX_END                  0
#define LIN_FLG_RX_END                  1
#define LIN_FLG_AWAKE                   2
#define LIN_FLG_DIAG_RESP               3
#define LIN_FLG_MAX                     4

/* Defines for LIN device to handle message direction.  */
#define LIN_MSG_TYPE_RX                0 //received data from sender for slave;
#define LIN_MSG_TYPE_TX                1 //data request from sender for slave;

/**
* @brief A structure to represent LIN frame information.
*/
typedef struct { 
    uint8_t  frame_id;   /*!< frame ID.  */ 
    uint8_t  msg_type;   /*!< Type of message, tx or rx.  */ 
    uint8_t  checksum;   /*!< Type of checksum (enhanced or classic).  */ 
    uint8_t  length;     /*!< Number of bytes in the data. */ 
    uint8_t  data[LIN_BUFF_SIZE];       /*!< data buffer. */
    uint8_t  responseValid;
} LIN_Device_Frame_t;

enum{
  LIN_RX = 0,
  LIN_TX
};

typedef void (*lin_cb_func_t)(LIN_Device_Frame_t *frame);

void LINS_Init(void);
void LINS_RegisterServices(LIN_Device_Frame_t *lin_cmd_table, uint8_t size);
void LINS_RegisterLINS_IRQ(lin_cb_func_t cb);
void LINS_UnRegisterLINS_IRQ(void);

void LINM_Init(void);
int8_t LINM_SendFrame(LIN_Device_Frame_t *frame);
void LINM_RegisterLINM_IRQ(lin_cb_func_t cb);
void LINM_UnRegisterLINM_IRQ(void);



#endif /* __LIN_DEVICE_H__ */
